<?php
/**
//说明：导入天眼查
//时间：2018-08-17 14:38:15
 */
defined('IN_IA') or exit('Access Denied');

global $_W, $_GPC;
load()->func('tpl');
$uniacid = $_W['uniacid'];
$opp = $_GPC['do'];
require dirname(__FILE__)."/Excel.class.php";

if ($_GPC['csv']) {
    $test = $_GPC['test'];
    //导入报名表
    $field_url = "attachment/".$_GPC['csv'];
    $uploadfilepath = str_replace('\\','/',realpath(dirname(dirname(dirname(dirname(dirname(__FILE__).'/'))))))."/".$field_url;
    $datas = Excel::getExcelFile(null, null, $uploadfilepath,12);//只要10列
    array_splice($datas,0,2);//第0列，第2行开始
    $total = 0;
    foreach ($datas as $key=>$val){
        if(empty($val[0])){
            continue;
        }
        $address = $val[5];
        // $url="http://apis.map.qq.com/ws/geocoder/v1/?address=".$address."&key=25MBZ-YMCC6-3A3S2-MUHF2-4ZJDO-HXB5K";
        // $jsondata=json_decode(file_get_contents($url),true);
        $mobile = explode(';',$val[6]);

        $data = array (
            'uniacid' => $_W['uniacid'],
            'company' => $val[0],
            'cardname' => $val[1],
            'credit' => $val[2],
            'createtime' => strtotime($val[3]),
            'mobile' => trim($mobile[0]),
            'address' => $val[7],
            'siteurl' => $val[8],
            'email' => $val[9],
            'desc' => $val[10],
            'job' => '总经理',
            'industry' => $_GPC['industry'],  
            'longitude' => $jsondata['result']['location']['lng'], 
            'latitude' => $jsondata['result']['location']['lat'], 
            'province' => $jsondata['result']['address_components']['province'],
            'city' => $jsondata['result']['address_components']['city'],
            'county' => $jsondata['result']['address_components']['district']
        );
        // 2018-7月版本
        // $data = array (
            // 'uniacid' => $_W['uniacid'],
            // 'company' => $val[0],
            // 'cardname' => $val[1],
            // 'credit' => $val[2],
            // 'createtime' => strtotime($val[3]),
            // 'mobile' => trim($mobile[0]),
            // 'address' => $val[5],
            // 'siteurl' => $val[6],
            // 'email' => $val[7],
            // 'desc' => $val[8],
            // 'job' => '总经理',
            // 'industry' => $_GPC['industry'],  
            // 'longitude' => $jsondata['result']['location']['lng'], 
            // 'latitude' => $jsondata['result']['location']['lat'], 
            // 'province' => $jsondata['result']['address_components']['province'],
            // 'city' => $jsondata['result']['address_components']['city'],
            // 'county' => $jsondata['result']['address_components']['district']
        // );

    if($test){
        echo "<BR>姓名：".$val[1];
        echo "<BR>\n公司：".$val[0];
        echo "<BR>\n注册资本：".$val[2];
        echo "<BR>\n手机号：".trim($mobile[0]);
        echo "<BR>\n地址：".$val[7];
        echo "<BR>\n经营范围；".$val[10];
        echo "<h1>\n请确认以上是否对应，特别是地址及经营范围；</h1>";
        die();
    }else{
        // echo '导入';
        // var_dump($jsondata);
        // var_dump($data);
        // $condition = "company='{$val[0]}'";
        // $findUser = pdo_fetch("SELECT id FROM " . tablename ('bbyscm_card') . " WHERE $condition");
        // if(!empty($findUser)){//已有则跳过
            // $result = pdo_update('bbyscm_card', $data,['company'=>$val[0]]);
            // continue;
            // echo "跳过 \n";
        // }else{
            $total +=1;
            // echo "导出 $total 条 \n";
            $result = pdo_insert('bbyscm_card', $data);
        // }
    }
    }
    // die();
    //var_dump($data);
    // echo json_encode(array("errno" => 1, 'message'=>'上传成功', 'data'=>$data));
    message('成功导入'.$total.'条数据，顶呱呱！', 'refresh');
}else{
    include $this->template('web/import');
}
